<script setup lang="ts">

import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import {ChooseExcel,SearchData} from "../../wailsjs/go/excel/ExcelService";
const keyword = ref("")
const filePath = ref<string>("")
function chooseFile(){
  ChooseExcel().then(res=>{
    filePath.value = res.data
  })
}
const sheetNames =ref<DataResult[]>([])
interface DataResult {
  sheetName : string
  columns:ColumnItem[],
  data:any[]
}
interface ColumnItem {
  label : string
  prop : string
}
const dataLoading = ref<boolean>(false)
async function search(){
  dataLoading.value = true
  SearchData(filePath.value , keyword.value).then(res =>{
    console.log("res >>>>> " ,res)
    dataLoading.value = false
    sheetNames.value = []
    if(res.code != 1){
      ElMessage.error(res.msg)
    }else{
      for(let key in res.data){
        let arr = res.data[key]
        let header = arr[arr.length-1]
        let items: ColumnItem[] = []
        let dataList : any[] = []
        header.forEach((item:string ,index : number)=>{
          items.push({
            label : item,
            prop : item
          })
        })

        for(let i = 0 ; i <arr.length - 1 ; i++){
          let dataObj :any = {}
          for(let j = 0 ; j < items.length; j ++){
            dataObj[items[j].label] = arr[i][j]
          }
          dataList.push(dataObj)
        }
        sheetNames.value.push({
          sheetName : key,
          columns : items,
          data:dataList
        })
      }
    }
    console.log(res)
  })
}


onMounted(()=>{

})
</script>

<template>
  <el-space>
    <el-button type="primary" @click="chooseFile">选择表格</el-button>

  </el-space>
  <el-space>
    <el-input placeholder="输入关键字查找" v-model="keyword" />
    <el-button type="primary" @click="search" :loading="dataLoading">搜索</el-button>
    <el-tag class="tag" v-if="filePath">{{filePath?.substring(filePath.lastIndexOf("/")+1)}}</el-tag>
  </el-space>
  <el-card class="mt-2" v-loading="dataLoading" style="min-height: 600px;">
    <el-card v-for="(item ,index) in sheetNames" :key="index" class="mt-2">
      <h4>{{item.sheetName}}</h4>
      <el-table :data="item.data" border size="small">
        <el-table-column v-for="(column ,ci ) in item.columns" :label="column.label" :prop="column.prop" :key="ci" width="120px" >
        </el-table-column>
      </el-table>
    </el-card>
  </el-card>

  <el-dialog>

  </el-dialog>
</template>

<style scoped>

</style>